package com.hongxun.budget.ui.fragment;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.alibaba.fastjson.JSONObject;
import com.hongxun.budget.ui.activity.BaseActivity;
import com.hongxun.budget.ui.activity.LoginActivity;
import com.hongxun.budget.utils.T;
import com.hongxun.entity.LoginInfo;
import com.hongxun.interfaces.NovaCallback;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import kr.co.namee.permissiongen.PermissionGen;

/**
 * Created by admin on 2018/5/15.
 */

public abstract class BaseFragment extends Fragment implements View.OnTouchListener, NovaCallback {

    public Context context;
    public View view;
    public boolean isPause;
    Unbinder unbinder;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = getActivity();
        view = inflater.inflate(getLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        setMapBundle(savedInstanceState);
        view.setOnTouchListener(this);
        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                initDault();
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        return view;
    }

    public void initDault() {
    }

    public void setMapBundle(Bundle bundle) {

    }


    public abstract int getLayoutId();

    public abstract void initView();


    @Override
    public void onPause() {
        super.onPause();
        isPause = true;
    }

    @Override
    public void onResume() {
        super.onResume();
        isPause = false;
    }


    /**
     * 消息提示
     *
     * @param msg
     */
    public void showMsg(String msg) {
        if (!isPause) {
            ((BaseActivity) getActivity()).showMsg(msg);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    @Override
    public void err(int i, int status, String err) {
        if ((status == 500 && err.equals("token失效，请重新登录"))
                || (status == 401 && err.equals("invalid token")) || (status == 500 && err.equals("token不能为空"))) {
            LoginInfo.remove(context);
            startActivity(new Intent(context, LoginActivity.class));
            T.showLong(context, err);
            getActivity().finish();
        } else {
            showMsg(err);
        }
    }

    @Override
    public void success(int i, JSONObject obj) {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(getActivity(), requestCode, permissions, grantResults);
    }
}